package com.test.nnooolwdeaer.dialog_wooden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseActivity_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.test.nnooolwdeaer.view_wooden.module_wooden.ShowWalletAccountListView_Cote_wooden;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.CoteBankAccountList_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class SubmitAccountInfoDialog_Cote_wooden extends BottomSheetDialog {

    private BaseActivity_wooden baseActivity_wooden;

    private ShowWalletAccountListView_Cote_wooden showWalletAccountListView_wooden;

    private final View.OnClickListener onSubmitBtnClickListener_wooden;

    private LinearLayout groupLayout_wooden;

    public SubmitAccountInfoDialog_Cote_wooden(@NonNull Context context, View.OnClickListener onSubmitBtnClickListener) {
        super(context, R.style.BottomSheetDialogStyle);
        if (context instanceof BaseActivity_wooden) baseActivity_wooden = (BaseActivity_wooden) context;
        this.onSubmitBtnClickListener_wooden = onSubmitBtnClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_submit_account_info_cote_wooden);
        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        init_wooden();
    }

    private void init_wooden(){

        findViewById(R.id.dialog_submit_account_info_ib_back).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("SUBMIT_ACCOUNT_INFO_DIALOG_COTE_CANCEL_CLICK");
            dismiss();
        });

        findViewById(R.id.dialog_submit_account_info_btn_submit).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("SUBMIT_ACCOUNT_INFO_DIALOG_COTE_SUBMIT_CLICK");
            if (onSubmitBtnClickListener_wooden !=null) onSubmitBtnClickListener_wooden.onClick(v);
            dismiss();
        });

        groupLayout_wooden = findViewById(R.id.dialog_submit_account_info_layout_group);

        ArrayList<CoteBankAccountList_wooden.CoteBankAccount> accounts = CoteBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden();
        for (CoteBankAccountList_wooden.CoteBankAccount account:accounts){
            addAccount_wooden(account);
        }

    }

    private void addAccount_wooden(CoteBankAccountList_wooden.CoteBankAccount bankAccount){
        View view = View.inflate(getContext(), R.layout.item_show_wallet_account_cote_wooden, null);
        TextView accountTv = view.findViewById(R.id.item_show_wallet_account_tv_account);
        ImageView accountTypeIv = view.findViewById(R.id.item_show_wallet_account_iv_wallet_type);
        TextView accountTypeTv = view.findViewById(R.id.item_show_wallet_account_tv_wallet_type);

        accountTv.setText(bankAccount.getWalletMobile());
        accountTypeTv.setText(bankAccount.getWalletDesc());

        //获取图标
        if (bankAccount.getBitmap()!=null){
            accountTypeIv.setImageBitmap(bankAccount.getBitmap());
        }else {
            ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
                try {
                    URL url = new URL(bankAccount.getWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        bankAccount.setBitmap(bitmap);
                        ThreadUtil_wooden.getInstance_wooden().runOnUiThread_wooden(() -> accountTypeIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    if (Constant_wooden.Debug) e.printStackTrace();
                }
            });
        }

        groupLayout_wooden.addView(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void show() {
        super.show();
        HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("SUBMIT_ACCOUNT_INFO_DIALOG_COTE_START");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("SUBMIT_ACCOUNT_INFO_DIALOG_COTE_END");
    }
}
